package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

/**
 * @author Xiao
 * @date 2021/7/17
 */
public class StudentDao implements StudentDao_Base{
    private static Student[] stuArr = new Student[5];

    //接收一个学生对象，将其存储到学生数组中，返回是否存储成功
    @Override
    public boolean addStudent(Student stu) {
        int index = -1;
        for (int i = 0; i < stuArr.length; i++) {
            if (stuArr[i] == null) {
                stuArr[i] = stu;
                index = i;
                break;
            }
        }
        if (index == -1) {
            return false;
        }
        stuArr[index] = stu;
        return true;
    }

    //返回学生数组
    @Override
    public Student[] findAllStudent() {
        return stuArr;
    }

    //根据学号删除学生数组中对应的学生对象
    @Override
    public void deleteStudent(String sid) {
        int index = getIndex(sid);
        stuArr[index] = null;
    }

    //接收要修改的学生信息，并修改
    @Override
    public void updateStudent(String sid, Student stu) {
        int index = getIndex(sid);
        stuArr[index] = stu;
    }

    //根据学号查找索引
    @Override
    public int getIndex (String sid) {
        int index = -1;
        for (int i = 0; i < stuArr.length; i++) {
            if (stuArr[i] != null && sid.equals(stuArr[i].getId())) {
                index = i;
                break;
            }
        }
        return index;
    }
}
